home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_brushedmetal2.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.0 KB  |  122 lines

  1. /*  IDbrushedmetal.sl written 9/99 by Ivan DeWolf
  2.  *  ivan@SpamSucks_martian-tools.com
  3.  *  feel free to copy, distribute, hack and/or abuse this code 
  4.  *  in any way you see fit, but please leave my name near the top
  5.  *
  6.  *  yet another brushedmetal. This one computes a specular 
  7.  *  highlight, and also can use a reflection map.
  8.  *  stochasticly spreads the map, so it looks a little grainy.
  9.  *  based somewhat on LGbrushedmetal.sl
  10.  *
  11.  *  if you set Kr = 0 and specwidth = 10
  12.  *  then assume specspread is roughness, then you have something 
  13.  *  very similar to plastic.sl...
  14.  *
  15.  *  PARAMETERS:
  16.  *  Ks            - coefficient of specular
  17.  *  Kd            - coefficient of diffuse
  18.  *  Ka            - coefficient of ambient
  19.  *  Kr            - coefficient of reflectivity
  20.  *  specwidth        - the width of the specular stripe
  21.  *               maxes out at 10
  22.  *  specspread        - the spread of the specular stripe
  23.  *              maxes out at .5
  24.  *  mapspread        - the spread of the image streaking
  25.  *              maxes out at .5
  26.  *  twist        - allows you to twist the direction of anisotropy
  27.  *              angle in radians (i.e. PI*.5 = 90 degrees)
  28.  *  mapname        - name of the environment map
  29.  *  specularcolor    - color of the specular hilight
  30.  */
  31.  
  32. color
  33. anisospecular (vector VA; float specspread; float specwidth)
  34. {
  35.  
  36.     extern vector I;
  37.     extern normal N;
  38.     extern point P;
  39.  
  40.     float nonspec;
  41.     vector V = normalize(-I);
  42.     normal NN = normalize(N);
  43.     normal Nf = faceforward(NN,-V);
  44.     color pixbrdf, C = 0;
  45.     point zro = 0;
  46.  
  47.     illuminance (P, Nf, PI*.5) {
  48.         extern vector L;  
  49.     extern color Cl;
  50.  
  51.         nonspec = 0;
  52.         lightsource ("__nonspecular", nonspec);
  53.         if (nonspec < 1) {
  54.             vector LN = normalize (L);
  55.         vector H = normalize (V + LN);
  56.  
  57.         pixbrdf  = specularbrdf(LN, Nf,V,specspread);
  58.             C += Cl * pixbrdf * pow( 1-abs(VA.H), 1/specwidth );
  59.         }
  60.     }
  61.     return C;
  62. }
  63.  
  64. surface
  65. k3d_brushedmetal2(
  66.     float    Ks        =  1,
  67.         Kd        = .01,
  68.         Ka        = .001,
  69.         Kr        = .6,
  70.         specwidth    = .5,
  71.         specspread     = .5,
  72.         mapspread     = .3,
  73.         twist        =  0;
  74.     string    mapname     = "";
  75.     color    specularcolor    =  1)
  76. {
  77.     point Po = transform("object",P);
  78.     color ev = 0;
  79.     vector D, V= normalize(-I);
  80.     vector Nf, Ntmp;
  81.         point zro = 0;
  82.     float i, numsamples = 20;
  83.     float angle, jitter;
  84.     float Jspread = PI*(1/numsamples)*mapspread;
  85.     vector VA = rotate(normalize(dPdu), twist, zro, normalize(N));
  86.     
  87.  
  88.     Nf = faceforward(normalize(N), -I);
  89.         Ntmp = Nf;
  90.  
  91.     if( mapname != "" ) {
  92.         for(i=0;i<=numsamples;i=i+1){
  93.           jitter = (random()-.5)*Jspread;
  94.           angle = PI*((i/numsamples)-.5)*mapspread;
  95.           Ntmp = rotate(Nf,angle+jitter,zro,VA);
  96.           D = reflect(-V, Ntmp);
  97.           D = vtransform("world", D);
  98.           ev += environment(mapname, D)*(.5-abs((i/numsamples)-.5))*.25;
  99.         }
  100.     } else
  101.         ev = 0;
  102.  
  103.     Oi = Os;
  104.     Ci = Oi * (Cs * (Ka * ambient() + Kd * diffuse(-Nf)) + 
  105.     specularcolor * Ks * anisospecular(VA, specspread*2, specwidth*.1)) +
  106.     ev * Kr;
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.